package com.bjpowernode.converter;
import com.bjpowernode.exception.MyDateException;
import org.springframework.core.convert.converter.Converter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class MyDateConverter implements Converter<String, LocalDate> {

    private String datePattern;
    private String errorMsg;

    public MyDateConverter(String datePattern, String errorMsg) {
        this.datePattern = datePattern;
        this.errorMsg = errorMsg;
    }

    @Override
    public LocalDate convert(String source) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(datePattern);
        LocalDate parse = null;
        try {
            parse = LocalDate.parse(source, dateTimeFormatter);
        } catch (DateTimeParseException e) {
            throw new MyDateException(errorMsg);
        }
        return parse;
    }
}
